<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="LVS,keepalived," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="keepalived是什么&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;keepalived是集群管理中保证集群高可用的一个服务软件，其功能类似于heartbeat，用来防止单点故障。 keepalived工作原理&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;keepalived是以VRRP协议为实">
<meta name="keywords" content="LVS,keepalived">
<meta property="og:type" content="article">
<meta property="og:title" content="keepalived 工作原理和配置说明">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/LVS/6. keepalived 工作原理和配置说明/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="keepalived是什么&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;keepalived是集群管理中保证集群高可用的一个服务软件，其功能类似于heartbeat，用来防止单点故障。 keepalived工作原理&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;keepalived是以VRRP协议为实">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-01T08:52:01.860Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="keepalived 工作原理和配置说明">
<meta name="twitter:description" content="keepalived是什么&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;keepalived是集群管理中保证集群高可用的一个服务软件，其功能类似于heartbeat，用来防止单点故障。 keepalived工作原理&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;keepalived是以VRRP协议为实">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/LVS/6. keepalived 工作原理和配置说明/"/>





  <title>keepalived 工作原理和配置说明 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/LVS/6. keepalived 工作原理和配置说明/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">keepalived 工作原理和配置说明</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:04:46+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="keepalived是什么"><a href="#keepalived是什么" class="headerlink" title="keepalived是什么"></a>keepalived是什么</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalived是集群管理中保证集群高可用的一个服务软件，其功能类似于heartbeat，用来防止单点故障。</p>
<h2 id="keepalived工作原理"><a href="#keepalived工作原理" class="headerlink" title="keepalived工作原理"></a>keepalived工作原理</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalived是以VRRP协议为实现基础的，VRRP全称Virtual Router Redundancy Protocol，即虚拟路由冗余协议。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;虚拟路由冗余协议，可以认为是实现路由器高可用的协议，即将N台提供相同功能的路由器组成一个路由器组，这个组里面有一个master和多个backup，master上面有一个对外提供服务的vip（该路由器所在局域网内其他机器的默认路由为该vip），master会发组播，当backup收不到vrrp包时就认为master宕掉了，这时就需要根据VRRP的优先级来选举一个backup当master。这样的话就可以保证路由器的高可用了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalived主要有三个模块，分别是core、check和vrrp。core模块为keepalived的核心，负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查，包括常见的各种检查方式。vrrp模块是来实现VRRP协议的。</p>
<h2 id="keepalived的配置文件"><a href="#keepalived的配置文件" class="headerlink" title="keepalived的配置文件"></a>keepalived的配置文件</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalived只有一个配置文件keepalived.conf，里面主要包括以下几个配置区域，分别是global_defs、static_ipaddress、static_routes、vrrp_script、vrrp_instance和virtual_server。</p>
<h3 id="global-defs区域"><a href="#global-defs区域" class="headerlink" title="global_defs区域"></a>global_defs区域</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主要是配置故障发生时的通知对象以及机器标识</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">global_defs &#123;</div><div class="line">    notification_email &#123;</div><div class="line">        a@abc.com</div><div class="line">        b@abc.com</div><div class="line">        ...</div><div class="line">    &#125;</div><div class="line">    notification_email_from alert@abc.com</div><div class="line">    smtp_server smtp.abc.com</div><div class="line">    smtp_connect_timeout 30</div><div class="line">    enable_traps</div><div class="line">    router_id host163</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>notification_email 故障发生时给谁发邮件通知。</li>
<li>notification_email_from 通知邮件从哪个地址发出。</li>
<li>smpt_server 通知邮件的smtp地址。</li>
<li>smtp_connect_timeout 连接smtp服务器的超时时间。</li>
<li>enable_traps 开启SNMP陷阱（<a href="https://en.wikipedia.org/wiki/Simple_Network_Management_Protocol" target="_blank" rel="external">Simple Network Management Protocol</a>）。</li>
<li>router_id 标识本节点的字条串，通常为hostname，但不一定非得是hostname。故障发生时，邮件通知会用到。</li>
</ul>
<h3 id="static-ipaddress和static-routes区域"><a href="#static-ipaddress和static-routes区域" class="headerlink" title="static_ipaddress和static_routes区域"></a>static_ipaddress和static_routes区域</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;static_ipaddress和static_routes区域配置的是是本节点的IP和路由信息。如果你的机器上已经配置了IP和路由，那么这两个区域可以不用配置。其实，一般情况下你的机器都会有IP地址和路由信息的，因此没必要再在这两个区域配置。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">static_ipaddress &#123;</div><div class="line">    10.210.214.163/24 brd 10.210.214.255 dev eth0</div><div class="line">    ...</div><div class="line">&#125;</div><div class="line">static_routes &#123;</div><div class="line">    10.0.0.0/8 via 10.210.214.1 dev eth0</div><div class="line">    ...</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上分别表示启动/关闭keepalived时在本机执行的如下命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">/sbin/ip addr add 10.210.214.163/24 brd 10.210.214.255 dev eth0</div><div class="line">/sbin/ip route add 10.0.0.0/8 via 10.210.214.1 dev eth0</div><div class="line">/sbin/ip addr del 10.210.214.163/24 brd 10.210.214.255 dev eth0</div><div class="line">/sbin/ip route del 10.0.0.0/8 via 10.210.214.1 dev eth0</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意： 请忽略这两个区域，因为我坚信你的机器肯定已经配置了IP和路由。</p>
<h3 id="vrrp-script区域"><a href="#vrrp-script区域" class="headerlink" title="vrrp_script区域"></a>vrrp_script区域</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用来做健康检查的，当时检查失败时会将vrrp_instance的priority减少相应的值。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">vrrp_script check_alive</div><div class="line">&#123;</div><div class="line">        script <span class="string">"/export/sh/check_nginx_alive.sh"</span></div><div class="line">        <span class="comment"># check every 2 seconds</span></div><div class="line">        interval 2</div><div class="line">        <span class="comment"># if failed, decrease 10 of the priority</span></div><div class="line">        weight -10</div><div class="line">        <span class="comment"># require 2 failures for failures</span></div><div class="line">        fail 2</div><div class="line">        <span class="comment"># require 1 sucesses for ok</span></div><div class="line">        rise 1</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上意思是如果script中的指令执行失败，那么相应的vrrp_instance的优先级会减少10个点。</p>
<h3 id="vrrp-instance和vrrp-sync-group区域"><a href="#vrrp-instance和vrrp-sync-group区域" class="headerlink" title="vrrp_instance和vrrp_sync_group区域"></a>vrrp_instance和vrrp_sync_group区域</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;vrrp_instance用来定义对外提供服务的VIP区域及其相关属性。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;vrrp_rsync_group用来定义vrrp_intance组，使得这个组内成员动作一致。举个例子来说明一下其功能：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两个vrrp_instance同属于一个vrrp_rsync_group，那么其中一个vrrp_instance发生故障切换时，另一个vrrp_instance也会跟着切换（即使这个instance没有发生故障）。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div></pre></td><td class="code"><pre><div class="line">vrrp_sync_group VG_1 &#123;</div><div class="line">    group &#123;</div><div class="line">        inside_network   <span class="comment"># name of vrrp_instance (below)</span></div><div class="line">        outside_network  <span class="comment"># One for each moveable IP.</span></div><div class="line">        ...</div><div class="line">    &#125;</div><div class="line">    notify_master /path/to_master.sh</div><div class="line">    notify_backup /path/to_backup.sh</div><div class="line">    notify_fault <span class="string">"/path/fault.sh VG_1"</span></div><div class="line">    notify /path/notify.sh</div><div class="line">    smtp_alert</div><div class="line">&#125;</div><div class="line">vrrp_instance VI_1 &#123;</div><div class="line">    state MASTER</div><div class="line">    interface eth0</div><div class="line">    use_vmac </div><div class="line">    dont_track_primary</div><div class="line">    track_interface &#123;</div><div class="line">        eth0</div><div class="line">        eth1</div><div class="line">    &#125;</div><div class="line">    mcast_src_ip </div><div class="line">    lvs_sync_daemon_interface eth1</div><div class="line">    garp_master_delay 10</div><div class="line">    virtual_router_id 1</div><div class="line">    priority 100</div><div class="line">    advert_int 1</div><div class="line">    authentication &#123;</div><div class="line">        auth_type PASS</div><div class="line">        auth_pass 12345678</div><div class="line">    &#125;</div><div class="line">    virtual_ipaddress &#123;</div><div class="line">        10.210.214.253/24 brd 10.210.214.255 dev eth0</div><div class="line">        192.168.1.11/24 brd 192.168.1.255 dev eth1</div><div class="line">    &#125;</div><div class="line">    virtual_routes &#123;</div><div class="line">        172.16.0.0/12 via 10.210.214.1</div><div class="line">        192.168.1.0/24 via 192.168.1.1 dev eth1</div><div class="line">        default via 202.102.152.1</div><div class="line">    &#125;</div><div class="line">    track_script &#123;</div><div class="line">        chk_http_port</div><div class="line">    &#125;</div><div class="line">    nopreempt</div><div class="line">    preempt_delay 300</div><div class="line">    debug</div><div class="line">    notify_master |</div><div class="line">    notify_backup |</div><div class="line">    notify_fault |</div><div class="line">    notify |</div><div class="line">    smtp_alert</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>notify_master/backup/fault 分别表示切换为主/备/出错时所执行的脚本。</li>
<li>notify 表示任何一状态切换时都会调用该脚本，并且该脚本在以上三个脚本执行完成之后进行调用，keepalived会自动传递三个参数（$1 = “GROUP”|”INSTANCE”，$2 = name of group or instance，$3 = target state of transition(MASTER/BACKUP/FAULT)）。</li>
<li>smtp_alert 表示是否开启邮件通知（用全局区域的邮件设置来发通知）。</li>
<li>state 可以是MASTER或BACKUP，不过当其他节点keepalived启动时会将priority比较大的节点选举为MASTER，因此该项其实没有实质用途。</li>
<li>interface 节点固有IP（非VIP）的网卡，用来发VRRP包。</li>
<li>use_vmac 是否使用VRRP的虚拟MAC地址。</li>
<li>dont_track_primary 忽略VRRP网卡错误。（默认未设置）</li>
<li>track_interface 监控以下网卡，如果任何一个不通就会切换到FALT状态。（可选项）</li>
<li>mcast_src_ip 修改vrrp组播包的源地址，默认源地址为master的IP。（由于是组播，因此即使修改了源地址，该master还是能收到回应的）</li>
<li>lvs_sync_daemon_interface 绑定lvs syncd的网卡。</li>
<li>garp_master_delay 当切为主状态后多久更新ARP缓存，默认5秒。</li>
<li>virtual_router_id 取值在0-255之间，用来区分多个instance的VRRP组播。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意： 同一网段中virtual_router_id的值不能重复，否则会出错，相关错误信息如下。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">Keepalived_vrrp[27120]: ip address associated with VRID not present <span class="keyword">in</span> received packet :</div><div class="line">one or more VIP associated with VRID mismatch actual MASTER advert</div><div class="line">bogus VRRP packet received on eth1 !!!</div><div class="line">receive an invalid ip number count associated with VRID!</div><div class="line">VRRP_Instance(xxx) ignoring received advertisment...</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以用这条命令来查看该网络中所存在的vrid：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tcpdump -nn -i any net 224.0.0.0/8</div></pre></td></tr></table></figure>
<ul>
<li>priority 用来选举master的，要成为master，那么这个选项的值最好高于其他机器50个点，该项取值范围是1-255（在此范围之外会被识别成默认值100）。</li>
<li>advert_int 发VRRP包的时间间隔，即多久进行一次master选举（可以认为是健康查检时间间隔）。</li>
<li>authentication 认证区域，认证类型有PASS和HA（IPSEC），推荐使用PASS（密码只识别前8位）。</li>
<li>virtual_ipaddress vip，不解释了。</li>
<li>virtual_routes 虚拟路由，当IP漂过来之后需要添加的路由信息。</li>
<li>virtual_ipaddress_excluded 发送的VRRP包里不包含的IP地址，为减少回应VRRP包的个数。在网卡上绑定的IP地址比较多的时候用。</li>
<li>nopreempt 允许一个priority比较低的节点作为master，即使有priority更高的节点启动。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先nopreemt必须在state为BACKUP的节点上才生效（因为是BACKUP节点决定是否来成为MASTER的），其次要实现类似于关闭auto failback的功能需要将所有节点的state都设置为BACKUP，或者将master节点的priority设置的比BACKUP低。我个人推荐使用将所有节点的state都设置成BACKUP并且都加上nopreempt选项，这样就完成了关于autofailback功能，当想手动将某节点切换为MASTER时只需去掉该节点的nopreempt选项并且将priority改的比其他节点大，然后重新加载配置文件即可（等MASTER切过来之后再将配置文件改回去再reload一下）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当使用track_script时可以不用加nopreempt，只需要加上preempt_delay 5，这里的间隔时间要大于vrrp_script中定义的时长。</p>
<ul>
<li>preempt_delay master启动多久之后进行接管资源（VIP/Route信息等），并提是没有nopreempt选项。</li>
</ul>
<h3 id="virtual-server-group和virtual-server区域"><a href="#virtual-server-group和virtual-server区域" class="headerlink" title="virtual_server_group和virtual_server区域"></a>virtual_server_group和virtual_server区域</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;virtual_server_group一般在超大型的LVS中用到，一般LVS用不过这东西，因此不多说。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></td><td class="code"><pre><div class="line">virtual_server IP Port &#123;</div><div class="line">    delay_loop </div><div class="line">    lb_algo rr|wrr|lc|wlc|lblc|sh|dh</div><div class="line">    lb_kind NAT|DR|TUN</div><div class="line">    persistence_timeout </div><div class="line">    persistence_granularity </div><div class="line">    protocol TCP</div><div class="line">    ha_suspend</div><div class="line">    virtualhost </div><div class="line">    alpha</div><div class="line">    omega</div><div class="line">    quorum </div><div class="line">    hysteresis </div><div class="line">    quorum_up |</div><div class="line">    quorum_down |</div><div class="line">    sorry_server </div><div class="line">    real_server  &#123;</div><div class="line">        weight </div><div class="line">        inhibit_on_failure</div><div class="line">        notify_up |</div><div class="line">        notify_down |</div><div class="line">        <span class="comment"># HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK</span></div><div class="line">        HTTP_GET|SSL_GET &#123;</div><div class="line">            url &#123;</div><div class="line">                path </div><div class="line">                <span class="comment"># Digest computed with genhash</span></div><div class="line">                digest </div><div class="line">                status_code </div><div class="line">            &#125;</div><div class="line">            connect_port </div><div class="line">            connect_timeout </div><div class="line">            nb_get_retry </div><div class="line">            delay_before_retry </div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>delay_loop 延迟轮询时间（单位秒）。</li>
<li>lb_algo 后端调试算法（load balancing algorithm）。</li>
<li>lb_kind LVS调度类型NAT/DR/TUN。</li>
<li>virtualhost 用来给HTTP_GET和SSL_GET配置请求header的。</li>
<li>sorry_server 当所有real server宕掉时，sorry server顶替。</li>
<li>real_server 真正提供服务的服务器。</li>
<li>weight 权重。</li>
<li>notify_up/down 当real server宕掉或启动时执行的脚本。</li>
<li>健康检查的方式，N多种方式。</li>
<li>path 请求real serserver上的路径。</li>
<li>digest/status_code 分别表示用genhash算出的结果和http状态码。</li>
<li>connect_port 健康检查，如果端口通则认为服务器正常。</li>
<li>connect_timeout,nb_get_retry,delay_before_retry分别表示超时时长、重试次数，下次重试的时间延迟。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其他选项暂时不作说明。</p>
<h2 id="keepalived主从切换"><a href="#keepalived主从切换" class="headerlink" title="keepalived主从切换"></a>keepalived主从切换</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主从切换比较让人蛋疼，需要将backup配置文件的priority选项的值调整的比master高50个点，然后reload配置文件就可以切换了。当时你也可以将master的keepalived停止，这样也可以进行主从切换。</p>
<h2 id="keepalived仅做HA时的配置"><a href="#keepalived仅做HA时的配置" class="headerlink" title="keepalived仅做HA时的配置"></a>keepalived仅做HA时的配置</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;请看该文档同级目录下的配置文件示例。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;10.210.214.113 为keepalived的备机，其配置文件为113.keepalived.conf</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;10.210.214.163 为keepalived的主机，其配置文件为163.keepalived.conf</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;10.210.214.253 为Virtual IP，即提供服务的内网IP地址，在网卡eth0上面</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.1.11 为模拟的提供服务的公网IP地址，在网卡eth1上面</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用tcpdump命令来捕获的结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">17:20:07.919419 IP 10.210.214.163&gt;224.0.0.18:VRRPv2,Advertisement, vrid 1, prio 200, authtype simple, intvl 1s, length 20</div></pre></td></tr></table></figure>
<h2 id="LVS-Keepalived配置"><a href="#LVS-Keepalived配置" class="headerlink" title="LVS+Keepalived配置"></a>LVS+Keepalived配置</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注Keepalived与LVS结合使用时一般还会用到一个工具ipvsadm，用来查看相关VS相关状态，关于ipvsadm的用法可以参考man手册。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;10.67.15.95为keepalived master，VIP为10.67.15.94，配置文件为95-lvs-keepalived.conf</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;10.67.15.96为keepalived master，VIP为10.67.15.94，配置文件为96-lvs-keepalived.conf</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;10.67.15.195为real server</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当使用LVS+DR+Keepalived配置时，需要在real server上添加一条iptables规则（其中dport根据情况添加或缺省）：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># iptables -t nat -A PREROUTING -p tcp -d 10.67.15.94 --dport 80 -j REDIRECT</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当使用LVS+NAT+Keepalived配置时，需要将real server的默认路由配置成Director的VIP10.67.15.94，必须确保client的请求是通过10.67.15.94到达real server的。</p>
<h2 id="安装keepalived"><a href="#安装keepalived" class="headerlink" title="安装keepalived"></a>安装keepalived</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从keepalived官网下载合适的版本，解压并执行如下命令完成安装。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># cd keepalived-xxx# ./configure --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --mandir=/usr/share# make &amp;&amp; make install</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以打成RPM包，然后安装。</p>
<h2 id="说明"><a href="#说明" class="headerlink" title="说明"></a>说明</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用到的HA场景如下： 两台主机host113和host163，内网IP在eth1网卡上，分别是10.210.214.113和10.210.214.163，VIP为公网IP在eth0上，IP地址是202.102.152.253，网关为202.102.152.1。当VIP在host113上提供服务时，host113上的默认路由为202.102.152.1，提供服务的端口为202.102.152.253:443。host113发生故障需要将VIP及服务切回到host163上的时候，需要以下几步，第一将VIP接管过来，第二添加默认路由202.102.152.1，第三启动在端口202.102.152.253:443上的服务。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如此一来，keepalived需要另外的脚本来完成添加默认路由和启动服务工作，这点和heartbeat中的resources是相同的。目前我进行了测试，发现keepalived速度要比heartbeat快，也就是说效率比heartbeat高。并且，最重要的一点，keepalived支持多个backup。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不要问为何有以上需求。要为两个不同的域名提供https服务，由于SSL证书问题，必须有两个公网IP地址分别绑定443端口。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当然，通过SNI也可以实现一个公网IP绑定443端口来为多个域名提供https服务，但是这需要浏览器支持（M$的IE浏览器不支持）。（nginx/apache）</p>
<h2 id="吐槽"><a href="#吐槽" class="headerlink" title="吐槽"></a>吐槽</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalived的主从切换比较让人蛋疼，需要修改配置文件或停止一方的运行。但是由于keepalived是通过vrrp协议来实现failover（故障转移）的，因此也决定了手动主从切换的不便。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalived的文档也很旧了，一直都找不到合适的文档，之我就一直忽略了vrrp_script这个区域，导致很多事情想不通。</p>
<p>参考资料</p>
<ol>
<li><a href="http://www.linuxvirtualserver.org/" target="_blank" rel="external">http://www.linuxvirtualserver.org/</a></li>
<li><a href="http://www.keepalived.org/LVS-NAT-Keepalived-HOWTO.html" target="_blank" rel="external">http://www.keepalived.org/LVS-NAT-Keepalived-HOWTO.html</a></li>
</ol>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/LVS/" rel="tag"># LVS</a>
          
            <a href="/tags/keepalived/" rel="tag"># keepalived</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/LVS/7. keepalived 和 lvs 的结合配置文件详解/" rel="next" title="keepalived 和 lvs 的结合配置文件详解">
                <i class="fa fa-chevron-left"></i> keepalived 和 lvs 的结合配置文件详解
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/LVS/8. LVS原理详解及部署之一：ARP原理准备/" rel="prev" title="LVS原理详解及部署之一：ARP原理准备">
                LVS原理详解及部署之一：ARP原理准备 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#keepalived是什么"><span class="nav-number">1.</span> <span class="nav-text">keepalived是什么</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#keepalived工作原理"><span class="nav-number">2.</span> <span class="nav-text">keepalived工作原理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#keepalived的配置文件"><span class="nav-number">3.</span> <span class="nav-text">keepalived的配置文件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#global-defs区域"><span class="nav-number">3.1.</span> <span class="nav-text">global_defs区域</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#static-ipaddress和static-routes区域"><span class="nav-number">3.2.</span> <span class="nav-text">static_ipaddress和static_routes区域</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vrrp-script区域"><span class="nav-number">3.3.</span> <span class="nav-text">vrrp_script区域</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vrrp-instance和vrrp-sync-group区域"><span class="nav-number">3.4.</span> <span class="nav-text">vrrp_instance和vrrp_sync_group区域</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#virtual-server-group和virtual-server区域"><span class="nav-number">3.5.</span> <span class="nav-text">virtual_server_group和virtual_server区域</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#keepalived主从切换"><span class="nav-number">4.</span> <span class="nav-text">keepalived主从切换</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#keepalived仅做HA时的配置"><span class="nav-number">5.</span> <span class="nav-text">keepalived仅做HA时的配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#LVS-Keepalived配置"><span class="nav-number">6.</span> <span class="nav-text">LVS+Keepalived配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装keepalived"><span class="nav-number">7.</span> <span class="nav-text">安装keepalived</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#说明"><span class="nav-number">8.</span> <span class="nav-text">说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#吐槽"><span class="nav-number">9.</span> <span class="nav-text">吐槽</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
